package com.howells.time.module.message.view;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.howells.time.R;
import com.howells.time.base.utils.ToastShow;
import com.howells.time.module.message.model.MessageMode;
import com.howells.time.module.message.presenter.MessagePresenter;
import com.howells.time.module.message.presenter.MessagePresenterImp;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

/**
 * Created by Administrator on 2016.5.8.
 */
public class MessageFragment extends Fragment implements MessageView,SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView mListview;
    private List<MessageMode> list;
    private MessageAdapter mListAdapter;
    private View view;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private MessagePresenter messagePresenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_message, container, false);
        init();
        messagePresenter=new MessagePresenterImp(this);
        return view;
    }

    private void init() {
        list=new ArrayList<>();
        adddata();
        mListview= (RecyclerView) view.findViewById(R.id.message_recycler);
        mListview.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        mListview.setItemAnimator(new SlideInLeftAnimator());
        mListAdapter =new MessageAdapter(view.getContext(),R.layout.item_message,list);
        mListview.setAdapter(mListAdapter);

        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.message_swipe);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        
        mListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                Toast.makeText(MessageFragment.this.getContext(), "position:" + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
                return false;
            }
        });
    }

    private void adddata() {
        MessageMode sm=new MessageMode("系统消息","2016-4-4","1111111111111111");
        list.add(sm);
        sm=new MessageMode("系统消息","2016-4-4","2222222222222222222222222222222222222222");
        list.add(sm);
        sm=new MessageMode("系统消息","2016-4-4","11111111111111113333333333333333333333333333333333333333333333333333");
        list.add(sm);
        sm=new MessageMode("系统消息","2016-4-4","0");
        list.add(sm);
        sm=new MessageMode("好友请求","2016-4-13","666666666666666666666666666666666666666666666666666666666666666");
        list.add(sm);
    }

    @Override
    public void resultMsg(String msg) {
        ToastShow.ToastShort(getContext(),msg);
    }

    @Override
    public void onRefresh() {

        //异步调用刷新
        //messagePresenter.refresh(null);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
                ToastShow.ToastShort(getContext(),"刷新成功");
            }
        },1000);
    }

    @Override
    public void addMessage(List messages) {
        MessageMode sm=new MessageMode("系统消息","2016-4-4","1111111111111111");
        list.add(sm);
        sm=new MessageMode("系统消息","2016-4-4","2222222222222222222222222222222222222222");
        list.add(sm);
    }
}
